package com.itheima.myBatisUtils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;


public class MyBatisUtils {

    private MyBatisUtils(){
    }
    private static SqlSessionFactory sqlSessionFactory;
    //1.加载核心配置文件
    static {
        try {
            //2.获取SqlSession工厂对象
            sqlSessionFactory  = new SqlSessionFactoryBuilder().build(   Resources.getResourceAsStream("MyBatisConfig.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //3.通过工厂对象获取SqlSession对象
    public  static  SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}
